//
//  MyViewController.m
//  中皮网
//
//  Created by ios程序员 on 15/10/29.
//  Copyright (c) 2015年 ios程序员. All rights reserved.
//

#import "MyViewController.h"
#import "MineHeaderView.h"
#import "MyZhangHuViewController.h"
#import "ShouCangViewController.h"
#import "MineTableCell.h"
#import "EditViewController.h"
#import "MaijiazhongxinViewController.h"

@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,strong) UIImageView *imageBack;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,copy) NSMutableArray *arr;
@property (nonatomic ,copy) NSString *string;
@property (nonatomic ,copy) NSString *nit;
@property (nonatomic ,strong) MineHeaderView *headerView;
@property (nonatomic ,strong) UIButton *shouCangButton;//收藏
@property (nonatomic ,strong) UIImageView *tXImageView;

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self tableViewSubViews];
    [self mineViews];
    [self shouCangs];
    [self yongHu];
    self.tableView.scrollEnabled = NO;
}

- (void)shouCangs
{
    self.shouCangButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shouCangButton.frame = CGRectMake(self.view.frame.size.width-100*WIDTH, 60*HEIGH, 60*WIDTH, 40*HEIGH);
    [self.shouCangButton setImage:[UIImage imageNamed:@"sc"] forState:UIControlStateNormal];
    [self.view addSubview:self.shouCangButton];
    [self.shouCangButton addTarget:self action:@selector(shouCangButtons) forControlEvents:UIControlEventTouchUpInside];
}

//收藏VC
- (void)shouCangButtons
{
    ShouCangViewController *shou = [[ShouCangViewController alloc] init];
    [self.navigationController pushViewController:shou animated:NO];
}

//用户信息
- (void)yongHu
{
    self.tXImageView = [[UIImageView alloc] initWithFrame:CGRectMake(25*WIDTH, 15*HEIGH, 80*WIDTH, 80*WIDTH)];
    [self.tXImageView setImage:[UIImage imageNamed:@"xx"]];
    self.tXImageView.layer.cornerRadius = 40*WIDTH;
    self.tXImageView.layer.borderWidth = 1;
    self.tXImageView.layer.masksToBounds = YES;
    [self.view addSubview:self.tXImageView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tXImageViewClick)];
    self.tXImageView.userInteractionEnabled = YES;
    [self.tXImageView addGestureRecognizer:tap];
    
}

- (void)mineViews
{
    self.headerView = [[MineHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    [self.tableView addSubview:self.headerView];
    
}

- (void)tXImageViewClick
{
    EditViewController *edit = [[EditViewController alloc] init];
    [self.navigationController pushViewController:edit animated:NO];
}

// tableView
- (void)tableViewSubViews
{
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *strpath = [cachesPath stringByAppendingPathComponent:@"location.txt"];
    
    NSError *error = nil;
    self.nit = [NSString stringWithContentsOfFile:strpath encoding:NSUTF8StringEncoding error:&error];
    if (self.nit == nil) {
        self.nit = @"1";
    }
    
    self.arr = @[@"首页", @"我的账户", @"买家中心", @"卖家中心", @"我的设计师"].mutableCopy;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, w, h - 20 * HEIGH) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.contentInset = UIEdgeInsetsMake(110 * HEIGH, 0, 0, 0);
    self.imageBack = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zx"]];
    self.imageBack.frame = CGRectMake(0, -110 * HEIGH, w, 110 * HEIGH);
//    self.tableView.rowHeight = 100;
    [self.tableView registerClass:[MineTableCell class] forCellReuseIdentifier:@"MyCellIndifer"];
    [self.tableView addSubview:self.imageBack];
}

// tableView协议方法
//
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 120 ;
    }
    else{
        return 100;
    }
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y  = scrollView.contentOffset.y;
    CGFloat x = (y + 110 * HEIGH) / 2;
    if (y < -110 * HEIGH) {
        self.imageBack.frame = CGRectMake(x, y, w + fabs(x) * 2, -y);
    }
}
// 每行cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55*WIDTH;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

// row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *myCell = @"MyCellIndifer";
    MineTableCell *cell = [tableView dequeueReusableCellWithIdentifier:myCell];
    
    //清除缓存//
    cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.1];
    if (indexPath.row == 0) {
//        NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//        CGFloat size = [self folderSizeAtPath:caches];
//        cell.textLabel.text = [NSMutableString stringWithFormat:@"%@ 缓存:%.2fM", [self.arr objectAtIndex:indexPath.row], size];
        cell.textLabel.text = self.arr[indexPath.row];
//        [cell.xianImage setImage:[UIImage imageNamed:@"xian.png"]];
        cell.xianImage.backgroundColor = [UIColor redColor];
        cell.imageView.image = [UIImage imageNamed:@"sy"];
        
} else if (indexPath.row == 1) {
        
        cell.textLabel.text = self.arr[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:@"zh"];
    } else if (indexPath.row == 2) {
        if ([self.nit isEqualToString:@"1"]) {
            
            cell.textLabel.text = self.arr[indexPath.row];
            cell.imageView.image = [UIImage imageNamed:@"mj"];
        }
        
        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSString *strpath = [cachesPath stringByAppendingPathComponent:@"location.txt"];
        
        NSError *error = nil;
        [self.nit writeToFile:strpath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    } else {
        cell.textLabel.text = self.arr[indexPath.row];
        if (indexPath.row == 3) {
            cell.imageView.image = [UIImage imageNamed:@"maijia"];
        } else if (indexPath.row == 4){
            cell.imageView.image = [UIImage imageNamed:@"sjs"];
        } else {
            cell.imageView.image = [UIImage imageNamed:@""];
        }
    }
    cell.accessoryType = 1;
    return cell;
}

// cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row ==2){
    MaijiazhongxinViewController *zhangHu = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"maijiazhongxin"];
        [self.navigationController pushViewController:zhangHu animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
